﻿using System;
using System.Drawing;
using System.Windows.Forms;
using TigerCapture.Core.Models;
using TigerCapture.Utils;
using SysResources = TigerCapture.Properties.Resources;

namespace TigerCapture.Presentation
{
    public partial class Operator : Form
    {
        private const string TimeFormatter = "{0:00}:{1:00}:{2:00}";

        private RecorderController _controller;
        private int _seconds;

        public Operator(RecorderController controller)
        {
            _controller = controller;

            InitializeComponent();

            Location = new Point(0, 0);

            btnPause.BackgroundImage = ImageHelper.ToGray((Bitmap)SysResources.Pause.Clone());
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            _controller.Start();

            timer.Start();

            btnStart.Enabled = false;
            btnPause.Enabled = true;

            btnStart.BackgroundImage = ImageHelper.ToGray((Bitmap)SysResources.Start.Clone());
            btnPause.BackgroundImage = SysResources.Pause;

            Location = new Point(0, -(Height - 2));
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            _controller.Pause();

            if (_controller.State == RecorderState.Paused)
            {
                timer.Stop();

                btnPause.BackgroundImage = SysResources.Continue;
            }
            else
            {
                Location = new Point(0, -(Height - 2));

                timer.Start();

                btnPause.BackgroundImage = SysResources.Pause;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer.Stop();

            _controller.Stop();

            Globals.ThisAddIn.Application.ActivePresentation.SlideShowWindow.View.Exit();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            _seconds++;

            lblTotalTime.Text = string.Format(TimeFormatter, _seconds / 3600, _seconds / 60, _seconds % 60);
        }

        private void Operator_MouseEnter(object sender, EventArgs e)
        {
            if (_controller.State == RecorderState.Recording &&
                Location.Y < 0)
            {
                btnPause_Click(sender, e);

                Location = new Point(0, 0);
            }
        }
    }
}
